import http from '@/anaf/request/index'


import {
	IS_LOGIN,
	USER_INFO,
	USER_DATA,
	MESSAGE_IDS,
	AGENT_INFO,
	AUTH_TYPE
} from '../types.js'

const state = {
	isLogin: uni.getStorageSync('isLogin') ? uni.getStorageSync('isLogin') : false,
	userInfo: uni.getStorageSync('userInfo') ? uni.getStorageSync('userInfo') : {},
	agentInfo: uni.getStorageSync('agentInfo') ? uni.getStorageSync('agentInfo') : {},
	userData: {}, //用户其他相关信息
	messageIds: {}, //小程序订阅消息模板ids
	authType: '', // smsLogin:手机号登录注册, accountLogin:密码登录, forgotPwd:忘记密码, changePwd:修改密码, bindMobile:绑定手机号
	
	circleData: [
	],
	//我的朋友
	firendList: [
	],
	//内置朋友圈相册banner图
	circleBgList:[
		]
}

const actions = {

	async login({commit},data){
		return new Promise((resolve, reject) => {
			http('users.login',data).then(res => {
				// console.log(res)
				if(res.status==200){
					commit('IS_LOGIN', true);
					commit('USER_INFO', res.data);
					uni.setStorageSync('token', res.token);
				}
				resolve(res)
				
		
			}).catch(e => {
				reject(e)
			});
		})
	},


	//签到
	async sign(){
		console.log("signsign==")
		return new Promise((resolve, reject) => {
			http('users.sign').then(res => {
				if(res.status==200){
					state.userInfo.point = parseInt(state.userInfo.point)+parseInt(res.data)
					this.commit('USER_INFO', state.userInfo);
				}
				resolve(res)
			}).catch(e => {
				reject(e)
			});
		})
	},

	
	// 获取用户信息
	getUserInfo({
		commit
	}, data = '') {
		console.log("getUserInfogetUserInfogetUserInfo===")
		return new Promise((resolve, reject) => {
			commit('IS_LOGIN', true);
			commit('USER_INFO', data['data']);
			// uni.setStorageSync('token', data['token']);
		
		})
	},
	
	

	// 退出登录
	logout({commit}) {
		uni.getStorageSync('token');
		uni.removeStorageSync('token');
		uni.removeStorageSync('session_key');
		uni.removeStorageSync('userInfo');
		uni.removeStorageSync('cartNum');
		uni.removeStorageSync('chatSessionId');
		uni.removeStorageSync('user_data');
		commit('IS_LOGIN', false);
		commit('USER_INFO', {});
	}
}

const mutations = {
	
	// 登录态
	[IS_LOGIN](state, data) {
		uni.setStorageSync('isLogin', data);
		state.isLogin = data;
	},
	// 用户信息
	[USER_INFO](state, data) {
		console.log("USER_INFO==")
		uni.setStorageSync('userInfo', data);
		state.userInfo = data;
	},
}

const getters = {

}

export default {
	state,
	mutations,
	actions,
	getters
}
